package com.zhang.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.common.lang.Result;
import com.zhang.entity.Blog;
import com.zhang.entity.Comment;
import com.zhang.entity.User;
import com.zhang.mapper.BlogMapper;
import com.zhang.service.BlogService;
import com.zhang.service.CommentService;
import com.zhang.service.UserService;
import com.zhang.utils.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张立松
 * @since 2023-04-20
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    CommentService commentService;
    @Autowired
    BlogService blogService;
    @Autowired
    UserService userService;

    @GetMapping("/get/{blogId}")
    public Result getCommentByBlogId(@PathVariable("blogId") Long blogId, @RequestParam(defaultValue = "1") Integer currentPage){
        //每页显示20条评论
        Page page = new Page(currentPage, 20);
        IPage iPage = commentService.page(page, new QueryWrapper<Comment>()
                                        .eq("blog_id", blogId).orderByDesc("created"));
        return Result.success(iPage);
    }

    //给博客添加评论
    @RequiresAuthentication     //登陆之后才能访问
    @PostMapping("/add")
    public Result addComment(@RequestBody Comment comment){
        if(comment.getContent().isEmpty()){
            return Result.fail("请输入评论");
        }
        User user = userService.getById(comment.getUserId());
        Blog blog = blogService.getById(comment.getBlogId());
        comment.setCreated(LocalDateTime.now());
        comment.setStatus(0);
        comment.setUserNickname(user.getNickname());
        comment.setUserAvatar(user.getAvatar());
        blog.setComment(blog.getComment()+1);
        commentService.save(comment);
        blogService.updateById(blog);   //博客评论数量+1
        return Result.success("添加成功");
    }

    //通过id删除评论
    @RequiresAuthentication     //登陆之后才能访问
    @PostMapping("/delete/{commentId}")
    public Result deleteByCommentId(@PathVariable("commentId") Long commentId){
        Comment comment = commentService.getById(commentId);
        Blog blog = blogService.getById(comment.getBlogId());
        Long loginUserId = ShiroUtil.getProfile().getId();
        //博客主人或评论主人或系统管理员都可以删除博客评论
        if(loginUserId.longValue() == blog.getUserId().longValue()
                || loginUserId.longValue() == comment.getUserId().longValue()
                || loginUserId.longValue() == 1l){
            blog.setComment(blog.getComment()-1);
            commentService.removeById(commentId);
            blogService.updateById(blog);   //博客评论数量-1
            return  Result.success("操作成功");
        }
        return Result.fail("您不能删除此评论");
    }
}

